<?php

namespace app\common\model\banxi\core;

use app\common\enums\AccountTypeEnum;
use app\common\model\banxi\course\Lexicon;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 我的词库
 */
class CoreMyLexicon extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_core_my_lexicon';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'accountTypeCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'studentId' => 'string',
        'isCollect' => 'boolean',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getAccountTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['accountType'] ?? '');
        return (new AccountTypeEnum())->getDescription($value) ?? '';
    }


    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    /**
     * 修改收藏
     *
     * @param string $accountId 账户ID
     * @param string $accountType 账户类型
     * @param int|null $studentId 学员ID
     * @param string $word 单词
     * @return bool 是否收藏
     */
    public
    function updateCollect(string $accountId, string $accountType, ?int $studentId, string $word): bool
    {
        // 先操作自己
        $isCollect = $this->updateCollectBase($accountId, $accountType, $word);

        if ($isCollect && $studentId) {
            // 如果是收藏需要操作学员的
            if (!$this->getCollectByAccount((string)$studentId, '3', $word)) { // 3代表学员类型
                $this->updateCollectBase((string)$studentId, '3', $word); // 3代表学员类型
            }
        }

        return $isCollect;
    }

    /**
     * 基础修改收藏方法
     *
     * @param string $accountId 账户ID
     * @param string $accountType 账户类型
     * @param string $word 单词
     * @return bool 是否收藏
     */
    protected
    function updateCollectBase(string $accountId, string $accountType, string $word): bool
    {
        if (empty($word)) {
            getResponseException('操作失败，单词不能为空', [], 1, 'ERROR');
        }

        $isCollect = $this->getCollectByAccount($accountId, $accountType, $word);

        if ($isCollect) {
            // 取消收藏
            $result = $this->update(['isCollect' => 0], [
                'accountId' => $accountId,
                'accountType' => $accountType,
                'word' => $word
            ]);
        } else {
            // 添加收藏
            $data = [
                'accountId' => $accountId,
                'accountType' => $accountType,
                'word' => $word,
                'isCollect' => 1
            ];

            if ($accountType == '3') { // 学员
                $data['studentId'] = (int)$accountId;
            } else {
                $data['studentId'] = $accountId;
            }

            $result = $this->insert($data);
        }

        return !$isCollect;
    }


    /**
     * 检查账户是否已收藏单词
     *
     * @param string $accountId 账户ID
     * @param string $accountType 账户类型
     * @param string $word 单词
     * @return bool 是否已收藏
     */
    public
    function getCollectByAccount(string $accountId, string $accountType, string $word): bool
    {
        $count = $this->where('accountId', $accountId)
            ->where('accountType', $accountType)
            ->where('word', $word)
            ->where('isCollect', 1)
            ->count();

        return $count > 0;
    }

    /**
     * 检查账户是否已收藏单词
     *
     * @param string|int $accountId 账户ID
     * @param string|int $accountType 账户类型
     * @param array $word 单词
     * @return array|\think\Collection|\think\model\Collection
     */
    public
    function getCollectByAccountList(string|int $accountId, string|int $accountType, array $word)
    {
        if (empty($word)) {
            return [];
        }
        return $this->where('accountId', $accountId)
            ->where('accountType', $accountType)
            ->where('word', $word)
            ->where('isCollect', 1)
            ->field('word')
            ->select();
    }

    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

    public
    function lexiconInfo()
    {
        return $this->hasOne(Lexicon::class, 'word', 'word');
    }
}